package com.zyn.rsgl.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zyn.rsgl.pojo.Meeting;
import com.zyn.rsgl.mapper.MeetingMapper;
import com.zyn.rsgl.pojo.User;
import com.zyn.rsgl.service.MeetingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyn.rsgl.utils.commonResult.R;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 会议表 服务实现类
 * </p>
 *
 * @author zcs
 * @since 2025-02-25
 */
@Service
public class MeetingServiceImpl extends ServiceImpl<MeetingMapper, Meeting> implements MeetingService {
    @Override
    public R listPage(Integer pageNum, Integer pageSize, String name, String timeStr, String status, String location) {
        // 创建查询器
        QueryWrapper<Meeting> queryWrapper = new QueryWrapper<>();
        // 处理时间参数转换
        LocalDateTime time = null;
        if (timeStr != null && !timeStr.isEmpty()) {
            try {
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); // 根据实际日期格式调整此模式
                time = LocalDateTime.parse(timeStr, formatter);
                queryWrapper.like("time", time);
            } catch (Exception e) {
                return R.fail("时间格式错误，请使用 'yyyy-MM-dd HH:mm:ss' 格式");
            }
        }
        if (name != null) {
            queryWrapper.like("name", name);
        }
        if (status != null) {
            queryWrapper.like("status", status);
        }
        if (location != null) {
            queryWrapper.like("location", location);
        }
//        增加排序
        queryWrapper.orderBy(true,false,"time");
        // 初始化分页查询
        Page<Object> page = PageHelper.startPage(pageNum, pageSize);
        // 执行查询
        List<Meeting> list = this.list(queryWrapper);
        if (list != null) {
            return R.ok(list, page.getTotal());
        }
        return R.fail("查询失败");
    }
}
